package org.example.terminal;

import org.example.dao.DepartmentDAO;
import org.example.service.DepartmentService;
import org.example.utils.DataUtils;
import org.example.utils.TableUtils;

import java.util.ArrayList;
import java.util.Scanner;

public class DepartmentMenu {
    public static void departmentMenu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("==================================");
        System.out.println("==========部门数据管理==============");
        System.out.println("===1、部门新增======================");
        System.out.println("===2、部门修改======================");
        System.out.println("===3、部门删除======================");
        System.out.println("===4、部门查询======================");
        System.out.println("===5、退出模块======================");
        System.out.println("==================================");
        System.out.println("请输入要执行的功能序号：");

        int operation = 0;
        try {
            operation = scanner.nextInt();
            switch (operation) {
                case 1:
                    add(scanner);
                    departmentMenu();
                    break;
                case 2:
                    update(scanner);
                    departmentMenu();
                    break;
                case 3:
                    delete(scanner);
                    departmentMenu();
                    break;
                case 4:
                    query();
                    departmentMenu();
                    break;
                case 5:
                    System.out.println("已退出部门管理！");
                    break;
                default:
                    System.out.println("您输入的功能序号不存在，请检查功能菜单并重新输入！");
                    departmentMenu();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            departmentMenu();
        }


    }

    public static void query() throws Exception {
        ArrayList<String[]> data = DepartmentService.queryDepartment();
        if (data != null && data.isEmpty()) {
            throw new Exception("======当前部门数据为空======");
        }

        if ( data != null){
            String[] header = {"序号","部门名称"};
            System.out.println("========部门数据查询========");
            TableUtils.printTable(header, DataUtils.listToStringArray(data));
        } else{
            throw new Exception("错误！请联系技术人员");
        }
    }

    private static void delete(Scanner scanner) throws Exception {
        System.out.println("========部门数据删除=========");
        ArrayList<String[]> departmentInfo = DepartmentService.getDepartmentInfo();
        if (departmentInfo == null || departmentInfo.isEmpty()){
            throw new Exception("=======部门数据为空=======");
        }
        DepartmentMenu.query();
        int num = 0;
        try {
            System.out.println("======请输入要删除的序号======");
            num = scanner.nextInt();
        } catch (Exception e) {
            throw new Exception("输入格式错误！请重新输入");
        }

        //序号范围校验
        if (num > departmentInfo.size() || num < 1) {
            throw new Exception("请正确输入部门对应的序号");
        }
        String id = departmentInfo.get(num - 1)[0];

        //service删除
        if (DepartmentService.deleteDepartment(id)){
            System.out.println("========部门数据删除成功！=========");
        }else{
            System.out.println("========部门数据删除失败！=========");
        }

    }

    private static void update(Scanner scanner) throws Exception {
        System.out.println("========部门数据修改=========");
        ArrayList<String[]> dptData = DepartmentDAO.getDepartmentInfo();
        if (dptData == null || dptData.isEmpty()){
            throw new Exception("=======部门数据为空======");
        }
        DepartmentMenu.query();

        try {
            System.out.println("======请输入要修改的序号======");
            int num = scanner.nextInt();
            ArrayList<String[]> data = DepartmentService.getDepartmentInfo();
            //序号范围校验
            if (data != null && (num > data.size() || num < 1)) {
                throw new Exception("请正确输入部门对应的序号");
            }
            String id = "";
            if (data != null) {
                id = data.get(num - 1)[0];
            }

            System.out.println("======请输入新部门的名称======");
            String newDepartName = scanner.next();

            if (newDepartName.matches("[\u4e00-\u9fa5]+")){
                //service修改
                if (DepartmentService.updateDepartment(id,newDepartName)){
                    System.out.println("========部门数据修改成功！=========");
                }else {
                    System.out.println("========部门数据修改成功！1=========");
                }
            }else{
                throw new Exception("输入的部门名称有错误，请输入符合要求的部门名称(部门名称为中文，不包含空格、字母、数字)");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public static void add(Scanner scanner) throws Exception {

        System.out.println("==================================");
        System.out.println("==========部门数据新增==============");
        System.out.println("===请输入部门名称：");
        String departName = "";
        try {
            departName = scanner.next();
        } catch (Exception e) {
            throw new Exception("输入不合法");
        }
        //使用正则表达式校验输入的departName必须为中文
        if (departName.matches("[\u4e00-\u9fa5]+")) {
            if (DepartmentService.queryDepartmentByName(departName)){
                throw new Exception("部门名称已存在");
            }
        } else {
            throw new Exception("输入的部门名称有错误,请输入符合要求的部门名称(部门名称为中文,不包含空格、字母、数字)");
        }

        //service新增
        if (DepartmentService.addDepartment(departName)) {
            System.out.println("=========部门新增成功！=========");
            System.out.println("==============================");
        } else {
            System.out.println("=====部门新增失败！请联系技术解决===");
        }
    }

}
